package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.gameserver.model.L2ItemInstance;

import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.network.serverpackets.WareHouseUpdate;

/**
 * 接收客户端操作仓库请求
 * @author Frank
 */
public class WareHouseRequest extends L2GameClientPacket {

	private static final String _C__B1_WAREHOUSEREQUEST = "[C] B1 WareHouseRequest";

	private static final Logger _log = LoggerFactory.getLogger(WareHouseRequest.class.getName());

	private static final byte INVENTORY2WAREHOUSE_REQUEST = 0; // -- 背包到仓库

	private static final byte WAREHOUSE2INVENTORY_REQUEST = 1; // -- 仓库到背包

	/**
	 * 操作类型：0表示将物品从背包存放到仓库；1表示将物品从仓库取出到背包中；2表示整理仓库请求；3表示仓库扩容请求
	 */
	private byte _type;

	private long _itemId;

	private byte _slot;

	private int _count;

	@Override
	protected void readImpl() {
		_type = readByte();
		_itemId = readLong();
		_slot = readByte();
		_count = readInt();
	}

	@Override
	protected void runImpl() {
		if (_type == INVENTORY2WAREHOUSE_REQUEST) {
			inventory2WareHouse();
		} else if (_type == WAREHOUSE2INVENTORY_REQUEST) {
			wareHouse2Inventory();
		}
	}

	/**
	 * 仓库移到背包
	 * @return
	 */
	private boolean wareHouse2Inventory() {
		boolean result = false;
		
		L2ItemInstance tempItem = getClient().getActiveChar().getWarehouse().getItemByObjectId(_itemId);

		if (!(tempItem != null && tempItem.isStackable() && getClient().getActiveChar().getInventory().validateCapacity(1))) {
			if (!getClient().getActiveChar().getInventory().validateCapacity(_count)) { //-- 背包满
//				getClient().getActiveChar().sendPacket(new SystemMessage(SystemMessageId.INVENTORY_IS_FULL));
				getClient().getActiveChar().sendMessage("您的背包空间不够，请扩充背包体积后再存放宝贝");
				return false;
			}
		}

		try {
			L2ItemInstance srcitem = getClient().getActiveChar().getWarehouse().getItemByObjectId(_itemId);
			
			if (srcitem != null) {
				byte sourceSlot = (byte) srcitem.getLocationSlot();
				L2ItemInstance item = getClient().getActiveChar().getWarehouse().transferItem("从仓库到背包", _itemId, _count, getClient().getActiveChar().getInventory(), getClient().getActiveChar(), null);

				if (item != null) {
					getClient().getActiveChar().sendPacket(new WareHouseUpdate(WareHouseUpdate.WAREHOUSE2INVENTORY, srcitem, item, sourceSlot));				
					result = true;
				} else {
					result = false;
				}
			} else {
				result = false;
			}
			
		} catch (Exception e) {
			result = false;
			e.printStackTrace();
		}
		if (!result) {
			getClient().getActiveChar().sendPacket(new SystemMessage(SystemMessageId.WAREHOUSE2INVENTORY_FAIL)); //-- 从仓库移到背包失败
		} else {
		}

		return result;
	}

	/**
	 * 背包移到仓库
	 * @return
	 */
	private boolean inventory2WareHouse() {
		boolean result = false;

		L2ItemInstance tempItem = getClient().getActiveChar().getInventory().getItemByObjectId(_itemId);

		if (!(tempItem != null && tempItem.isStackable() && getClient().getActiveChar().getWarehouse().validateCapacity(1))) {
			if (!getClient().getActiveChar().getWarehouse().validateCapacity(_count)) { //-- 仓库满
//				getClient().getActiveChar().sendPacket(new SystemMessage(SystemMessageId.INVENTORY_IS_FULL));
				getClient().getActiveChar().sendMessage("您的仓库空间不够，请扩充仓库体积后再存放宝贝");
				return false;
			}
		}

		try {
			L2ItemInstance srcitem = getClient().getActiveChar().getInventory().getItemByObjectId(_itemId);
			
            if (srcitem != null) {
            	byte sourceSlot = (byte) srcitem.getLocationSlot();
    			L2ItemInstance item = getClient().getActiveChar().getInventory().transferItem("从背包到仓库", _itemId, _count, getClient().getActiveChar().getWarehouse(), getClient().getActiveChar(), null);

    			if (item != null) {
    				getClient().getActiveChar().sendPacket(new WareHouseUpdate(WareHouseUpdate.INVENTORY2WAREHOUSE, srcitem, item, sourceSlot));
    				
    				result = true;
    			} else {
    				result = false;
    			}
			} else {
				result = false;
			}
			
		} catch (Exception e) {
			result = false;
			e.printStackTrace();
		}
		if (!result) {
			getClient().getActiveChar().sendPacket(new SystemMessage(SystemMessageId.INVENTORY2WAREHOUSE_FAIL)); //-- 从背包移到仓库失败
		} else {
		}

		return result;
	}

	@Override
	public String getType() {
		return _C__B1_WAREHOUSEREQUEST;
	}
}
